<?php
define('GF_ERR_NONAUTHORIZED', 1);
define('GF_ERR_WRONGTOKEN', 2);
define('GF_ERR_FETCHERROR', 3);
class Greatiful {
	protected $token = null;
	protected $token_secret = null;
	protected $verifier = null;
	protected $consumer_key = GF_CONSUMER_KEY;
	protected $consumer_secret = GF_CONSUMER_SECRET;
	protected $oauth = null;

	protected $requestTokenUrl = GF_REQUEST_TOKEN_URL;
	protected $authorizeUrl = GF_AUTHORIZE_URL;
	protected $accessTokenUrl = GF_ACCESS_TOKEN_URL;
	protected $apiUrl = GF_API_URL;

	protected $error = 0;

	function __construct($token = null, $verifier = null, $secret = null) {
		if (session_id() == '') session_start();
		$this->oauth = new OAuth($this->consumer_key, $this->consumer_secret);
		if ($token == null) {
			if (!empty($_SESSION['gf_token'])) {
				$this->token = $_SESSION['greatiful']['oauth_token']; 
				$this->token_secret = $_SESSION['greatiful']['oauth_token_secret'];
				//$this->setToken($this->token, $this->token_secret);
			}
		} else {
			$this->token = $token; 
			$this->token_secret = $secret?$secret:(isset($_SESSION['greatiful'])?$_SESSION['greatiful']['oauth_token_secret']:null);
			$this->verifier = $verifier;
			//$this->setToken($this->token, $this->token_secret);
		}
		return $this->token;
	}
	
	public function getAuthorizeUrl() {
	    $token = $this->oauth->getRequestToken($this->requestTokenUrl);
	    if ($token) {
		$_SESSION['greatiful'] = $token;
		$this->token = $token['oauth_token'];
		$this->token_secret = $token['oauth_token_secret'];
		return $this->authorizeUrl."?oauth_token={$token['oauth_token']}";
	    }
	    return false;
	}

	public function getError() {
		return $this->error;
	}

	public function getLastResponse() {
		return $this->oauth->getLastResponse();
	}
	
	public function getLastResponseInfo() {
		return $this->oauth->getLastResponseInfo();
	}
	
	public function getMethod($parts, $params = array(), $method = OAUTH_HTTP_METHOD_GET, $format = 'json') {
		$this->error = 0;
		if ($this->token == null) {
			$this->error = GF_ERR_NONAUTHORIZED;
			return false;
		}
		$this->oauth->setToken($this->token, $this->token_secret);
		try {
			$token = $this->oauth->getAccessToken($this->accessTokenUrl.'?oauth_verifier='.$this->verifier);
		} catch (OAuthException $e) {
		    $this->error = GF_ERR_NONAUTHORIZED;
		    return false;
		}
		if ($token == false) {
			$this->error = GF_ERR_NONAUTHORIZED;
			return false;
		}
		$this->oauth->setToken($token['oauth_token'], $token['oauth_token_secret']);
		$url = $this->apiUrl.'/'.implode('/', $parts).'.'.$format;
		try {
			if (!$this->oauth->fetch($url, $params, $method)) {//, null, $method)) {
				$this->error = GF_ERR_FETCHERROR;
				return false;
			}
		} catch (OAuthException $e) {
			$this->error = GF_ERR_FETCHERROR;
			return false;
		}
		return json_decode($this->oauth->getLastResponse());
	}

	public function postMethod($parts, $params = array(), $format = 'json') {
		return $this->getMethod($parts, $params, OAUTH_HTTP_METHOD_POST, $format);
	}

        public function deleteMethod($parts, $params = array(), $format = 'json') {
                return $this->getMethod($parts, $params, OAUTH_HTTP_METHOD_POST, $format);
        }

} 
